// author: hoang.nguyenmau
//this file is template for activity same as cGame
//GameLib same GLIb
package gameEngine.com;

import gameEngine.com.state.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.*;
import android.view.View.OnKeyListener;
import android.graphics.*;
import android.content.*;

public class MainActivity extends GameLib {
	public static boolean start = true;
	public static boolean running = true;
	public static BitmapFont fontsmall = null;
	public static Bitmap splashImage;
	public static Bitmap logoimage;
	public static Sprite spriteIcon = null;
	protected static Sprite spriteMenuButton;
	protected static Sprite spriteMenu;
	protected static boolean isGamePause = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mainView = new PanelView(this);
		DisplayMetrics metrics = mainView.getResources().getDisplayMetrics();
		GameLib.SCREEN_WIDTH = SCREEN_WIDTH = metrics.widthPixels;
		GameLib.SCREEN_HEIGHT = SCREEN_HEIGHT = metrics.heightPixels;

		setContentView(mainView, new ViewGroup.LayoutParams(SCREEN_WIDTH,
				SCREEN_HEIGHT));

		// Log.d("SCREEN_WIDTH : ", " " + SCREEN_WIDTH);
		// Log.d("SCREEN_HEIGHT : ", " " + SCREEN_HEIGHT);

		(new Thread(new GameThread())).start();
		mainGameLib = this;
		changeState(STATE_LOGO);
	}

	public static void changeState(int nextstate) {
		// SendMessage
		resetTouch();
		SendMessage(mCurrentState, MESSAGE_DTOR);
		mPreState = mCurrentState;
		mCurrentState = nextstate;
		SendMessage(nextstate, MESSAGE_CTOR);
		// reset variable
		timeBeginCurrentState = System.currentTimeMillis();// L not 1
		frameCountCurrentState = 0;// L not 1

	}

	public static synchronized void SendMessage(int state, int type) {
		switch (state) {
		case STATE_LOGO:
			StateLogo.SendMessageStateLogo(type);
			break;
		case STATE_ASKSOUND:
			break;
		case STATE_SPLASH:
			StateSplash.SendMessageStateSplash(type);
			break;
		case STATE_MAINMENU:
			StateMainMenu.SendMessageStateMainMenu(type);
			break;
		case STATE_ABOUT:
			StateAbout.SendMessageStateAbout(type);
			break;
		case STATE_HELP:
			StateHelp.SendMessageStateHelp(type);
			break;
		case STATE_GAMEPLAY:
			StateGameplay.SendMessageGameplay(type);
			break;
		case STATE_IGM:
			StateIGM.SendMessageStateIGM(type);
			break;
		case STATE_LOADING:
			StateLoading.SendMessageStateLoading(type);
			break;
		case STATE_COMPLETE_LEVEL:
			StateEndLevel.SendMessageStateStateEndLevel(type);
			break;
		case STATE_SELECT_LEVEL:
			StateSelectLevel.SendMessageStateSelectLevel(type);
			break;		
			
		}

	}

	@Override
	public synchronized void DrawAll(Canvas canvas, Paint paint) {
		mainCanvas = canvas;
		mainPaint = paint;
		SendMessage(mCurrentState, MESSAGE_PAINT);
		canvas.restore();

	}

}